package com.leukim.ssk.client;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SSKActivity extends Activity {
	
	private SharedPreferences mPreferences;
	
	public static final int SSK_UI_MAIN = 1;
	public static final int SSK_UI_GAME = 2;
	
	public static final int GAME_CREATE = 1;
	public static final int SETTINGS_EDIT = 2;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mPreferences = getSharedPreferences("com.leukim.ssk.client", MODE_PRIVATE);
        
        setupUI(SSK_UI_MAIN);
        refreshUsername();
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings: //Settings
			Intent i = new Intent(getApplicationContext(), Settings.class);
			startActivityForResult(i, SETTINGS_EDIT);
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		switch (requestCode) {
		case SETTINGS_EDIT:
			switch (resultCode) {
			case RESULT_OK:
				Toast t_ok = Toast.makeText(getApplicationContext(), R.string.settingssaved, Toast.LENGTH_SHORT);
				t_ok.show();
				refreshUsername();
				break;
			case RESULT_CANCELED:
				break;
			default:
				Toast t_err = Toast.makeText(getApplicationContext(), R.string.savefailed, Toast.LENGTH_SHORT);
				t_err.show();
			}
			break;
			
		case GAME_CREATE:
			switch (resultCode) {
			case RESULT_OK:
				Toast t_ok = Toast.makeText(getApplicationContext(), "This would create a game!", Toast.LENGTH_SHORT);
				t_ok.show();
				break;
			case RESULT_CANCELED:
				break;
			default:
				Toast t_err = Toast.makeText(getApplicationContext(), R.string.gamecreatefailed, Toast.LENGTH_SHORT);
				t_err.show();
			}
			break;
		}
	}
	
	protected void refreshUsername() {
        TextView tv_username = (TextView) findViewById(R.id.tv_current_username);
        String user = mPreferences.getString("username", null);
        
        if (user == null) {
        	tv_username.setText(R.string.anonymoususer);
        } else {
        	tv_username.setText(user);
        }
	}
	
	protected void setupUI(int view) {
		switch (view) {
		case SSK_UI_MAIN:
			setContentView(R.layout.main);
	        Button btn_joingame = (Button) findViewById(R.id.btn_joingame);
	        btn_joingame.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					return;
				}
			});
	        
	        Button btn_newgame = (Button) findViewById(R.id.btn_newgame);
	        btn_newgame.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent i = new Intent(getApplicationContext(), GameCreate.class);
					startActivityForResult(i, GAME_CREATE);
				}
			});
	        break;
	    default:
	        break;
		}
	}
	
}